﻿using Microsoft.EntityFrameworkCore;
using Models.Common;
using GatewayServerService.CloudCenter;

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GatewayServerService.Data;
using UtilsORM;
using UtilsORM.MySqlData;
using Models.Constant;
using Newtonsoft.Json;
using System.Diagnostics;
using UtilsMQ.Model;
using GatewayServerService.Mq;

namespace GatewayServerService.Mq
{
    public class ServiceSendMQ
    {
        /// <summary>
        /// 发送设备指令
        /// </summary>
        /// <param name="queuename"></param>
        /// <param name="commandLogModel"></param>
        /// <returns></returns>
        public bool sendCommandToGateWay(CommandLogModel commandLogModel)
        {
            //发送mq
            try
            {
                string queuename = MQEventEnum.MQDevice_gatewayServer_QueueName + commandLogModel.gatewayCode;
                MQReqModel<object> mQReqModel = new MQReqModel<object>();
                mQReqModel.respEvent = commandLogModel.mqevent;
                mQReqModel.respCode = ResponseCode.SUCCESS;
                mQReqModel.gatewayCode = commandLogModel.gatewayCode;
                mQReqModel.gatewayDeviceGuid = commandLogModel.gatewayDeviceGuid;
                mQReqModel.toGatewayData = commandLogModel;
                mQReqModel.guid = Guid.NewGuid().ToString();


                return new ServiceSendMQ().sendMsg(queuename, mQReqModel);

            }
            catch (Exception ex)
            {
                Console.WriteLine("sendMsg Exception： " + ex);
                return false;
            }


        }
        public bool sendMsg(string queuename, MQReqModel<object> mQReqModel, string toAppName = ConstantAppName.gateway)
        {
            mQReqModel.createTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
            string msg = JsonConvert.SerializeObject(mQReqModel);
            //Console.WriteLine("############################################################");
            //Console.WriteLine("下发网关code:" + mQReqModel.gatewayCode);
            //Console.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString());
            //Console.WriteLine("下发mq消息推送到网关:" + msg);
            //Console.WriteLine("############################################################");


            if (toAppName == ConstantAppName.gateway)
            {
                //根据配置信息判断是否dtu
                //if(mQReqModel.gateWayType=="dtu")
                //{
                //     new ServiceMQTT_Client_Helper_DTU().sendMsg("dtu_"+mQReqModel.gatewayCode, mQReqModel);

                //}
                //else
                //{
                new ServiceMQTT_Client_Helper().sendMsg(ConstantMqtt.gateway_topic + "_" + mQReqModel.gatewayCode, msg);
                // }


                Debug.WriteLine("####################" + ConstantMqtt.gateway_topic + "-mqtt通道########################################");
            }
            else if (toAppName == ConstantAppName.gateway_upgrade)
            {
                new ServiceMQTT_Client_Helper().sendMsg(ConstantMqtt.gateway_upgrade_topic + "_" + mQReqModel.gatewayCode, msg);
                Debug.WriteLine("####################" + ConstantMqtt.gateway_upgrade_topic + "-mqtt通道########################################");
            }
            ///写日志
            new DBIotMqLog().writeMQLog(msg, "send", "true");
            return true;
        }




        /// <summary>
        /// 发送原始指令
        /// </summary>
        /// <param name="content"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public void sendCommand(object content, string gwcode, string eventName, string toAappName = ConstantAppName.gateway)
        {
            string sql = "select * from iot_device_gateway  where gw_code='" + gwcode + "'";
            DataTable dt = MySqlYihongIot.GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                MQReqModel<object> mQReqModel = new MQReqModel<object>();
                mQReqModel.respEvent = eventName;
                mQReqModel.respCode = ResponseCode.SUCCESS;
                //mQReqModel.gateWayDeviceId = deviceId;
                mQReqModel.gatewayCode = dt.Rows[0]["gw_code"].ToString();
                mQReqModel.gatewayDeviceGuid = dt.Rows[0]["gw_d_guid"].ToString();
                mQReqModel.data = content;
                mQReqModel.guid = Guid.NewGuid().ToString();
                new ServiceSendMQ().sendMsg(MQEventEnum.MQDevice_gatewayServer_QueueName + mQReqModel.gatewayCode, mQReqModel, toAappName);
            }
        }


    }
}